package com.telenav.techtalk;

import android.app.Activity;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.EmbossMaskFilter;
import android.graphics.MaskFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

/**
 * @author yxyao
 *
 */
public class PathEffectActivity extends Activity {
    /** Called when the activity is first created. */
	
	private CanvasView cv;
	
	private MaskFilter mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
             0.4f, 6, 3.5f);

	private MaskFilter mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

	private PathEffect mDash = new DashPathEffect(new float[]{20.0f, 20.0f}, 2);
	
	private PathEffect mRound = new CornerPathEffect(20);
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.effect_view);
//        setContentView(new PathEffectView(this),new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        cv = (CanvasView)findViewById(R.id.canvasview);
        OnCheckListener listener = new OnCheckListener();
        RadioButton emboss = (RadioButton)findViewById(R.id.emboss);
        emboss.setOnCheckedChangeListener(listener);
        RadioButton blur = (RadioButton)findViewById(R.id.blur);
        blur.setOnCheckedChangeListener(listener);
        RadioButton dash = (RadioButton)findViewById(R.id.dash);
        dash.setOnCheckedChangeListener(listener);
        RadioButton round = (RadioButton)findViewById(R.id.roundrect);
        round.setOnCheckedChangeListener(listener);
        
        RadioButton nomask = (RadioButton)findViewById(R.id.nomask);
        nomask.setOnCheckedChangeListener(listener);
        
        RadioButton nopath = (RadioButton)findViewById(R.id.nopatheffect);
        nopath.setOnCheckedChangeListener(listener);
        Button clean = (Button)findViewById(R.id.clean);
        clean.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				cv.clean();
			}
		});
        
    }    
    class OnCheckListener implements OnCheckedChangeListener{
    	
    	
    	public void onCheckedChanged(CompoundButton buttonView,
    			boolean isChecked) {
    		if(isChecked){
    			switch (buttonView.getId()) {
				case R.id.emboss:		
						cv.getPaint().setMaskFilter(mEmboss);
					break;
				case R.id.blur:
						cv.getPaint().setMaskFilter(mBlur);
					break;
				case R.id.dash:
						cv.getPaint().setPathEffect(mDash);
					break;
				case R.id.roundrect:
						cv.getPaint().setPathEffect(mRound);
					break;
				case R.id.nomask:
					cv.getPaint().setMaskFilter(null);
					break;
				case R.id.nopatheffect:
					cv.getPaint().setPathEffect(null);
					break;
				default:
					break;
				}
    		}
    	}
    }
    
    class PathEffectView extends View{
    	public PathEffectView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
		}

		@Override
    	protected void onDraw(Canvas canvas) {
			Paint p = new Paint();
			p.setColor(0xFFFF0000);
			p.setAntiAlias(true);
			p.setStyle(Style.FILL);
			PathEffect peff = new DiscretePathEffect(5.0f, 5.0f);
			p.setPathEffect(peff);
			canvas.drawLine(10, 10, getWidth(), 10, p);
			
			peff = new DashPathEffect(new float[]{5.0f, 5.0f}, 2);
			p.setPathEffect(peff);
			canvas.drawLine(10, 30, getWidth(), 30, p);
			
			peff = new CornerPathEffect(5);
			p.setPathEffect(peff);
			canvas.drawRect(new Rect(10, 50, getWidth()-10, 150), p);
    	}
		
		public boolean onTouchEvent(MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_DOWN){
				invalidate();
			}
			return super.onTouchEvent(event);
		}
    }
}